openCV Configure for Raspberry PI

What is openCV?

  • Collection of computer vision tools in one place
  • Computational photography to object detection

Where is openCV?

What resources did I use?

The step by step of getting it going.

  1. Make sure we have enough room.
  • df -h
  • expand the file system with
  • sudo raspi-config
  1. Make room with removing the wolfram image
  • sudo apt-get purge wolfram-engine

Install the tools

  1. Dependencies
    sudo apt-get update
    sudo apt-get upgrade

Make sure all the dev depencies for Python are installed

sudo apt-get install python2.7-dev python3-dev

CMake is needed

sudo apt-get install build-essential cmake pkg-config

Image file support

sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev

Video I/O packages

sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev

highGUI gto depedencies

sudo apt-get install libgtk2.0-dev

FORTRAN optimation matrix

sudo apt-get install libatlas-base-dev gfortran

In [ ]:
## Get the source code openCV 3.2

Create a directory
```
cd ~
mkdir opencv
```

```
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.2.0.zip
unzip opencv.zip
```

```
wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.2.0.zip
unzip opencv_contrib.zip

```

setup virtualenv

sudo pip3 install virtualenv virtualenvwrapper
sudo rm -rf ~/.cache/pip

Add this to your .profile

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

Create the virtualenv for opencv for python3

mkvirtualenv cv -p python3

Update the environment

source ~/.profile
workon cv

Now you are ready to start compiling.

Set up python in the virtualenv

  • Good place to start running tmux

Make sure you see the prompt:

(cv) pi@cvpi:~/opencv $

Install numpy

pip3 install numpy

compile and isntall opencv

  • get tmux going
workon cv
cd ~/opencv/opencv-3.2.0/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib-3.2.0/modules \
    -D BUILD_EXAMPLES=ON ..

finally nmake it

make -j4
sudo make install
sudo ldconfig
(cv) pi@cvpi:~/opencv/opencv-3.2.0/build $ ls -l /usr/local/lib/python3.4/site-p
ackages/
total 3212
-rw-r--r-- 1 root staff 3287708 Feb 12 04:35 cv2.cpython-34m.so
Do you really want to exit ([y]/n)? y                                  [11/1984]
(cv) pi@cvpi:~/opencv/opencv-3.2.0/build $ cd /usr/local/lib/python3.4/site-pack
ages/
(cv) pi@cvpi:/usr/local/lib/python3.4/site-packages $ sudo mv cv2.cpython-34m.so
 cv2.so
(cv) pi@cvpi:/usr/local/lib/python3.4/site-packages $ cd ~/.virtualenvs/cv/lib/p
ython3.4/site-packages/
(cv) pi@cvpi:~/.virtualenvs/cv/lib/python3.4/site-packages $ ln -s /usr/local/li
b/python3.4/site-packages/cv2.so cv2.so
(cv) pi@cvpi:~/.virtualenvs/cv/lib/python3.4/site-packages $ source ~/.profile
pi@cvpi:~/.virtualenvs/cv/lib/python3.4/site-packages $ cd
pi@cvpi:~ $ workon cv
bject?   -> Details about 'object', use 'object??' for extra details.

In [1]: import cv2

In [2]: cv2.__version__
Out[2]: '3.2.0'